import sqlite3


class DBUtil:

    def __init__(self, cursor):
        self.cursor = cursor
        cursor.execute('create table person (name varchar(20), sex number)')

    def insert(self, person):
        self.cursor.execute('insert into person (name, sex) values(\'{0}\', \'{1}\')'.format(person.name, person.sex))

    def delete(self, person):
        self.cursor.execute('delete from person where name=\'{0}\''.format(person.name))
        print('rowcount={0}'.format(self.cursor.rowcount))

    def update(self, person):
        self.cursor.execute('update person set sex=\'{0}\' where name=\'{1}\''.format(person.sex, person.name))
        print('rowcount={0}'.format(self.cursor.rowcount))

    def query(self):
        self.cursor.execute('select * from person')
        ps = self.cursor.fetchall()
        print(ps)


class Person:
    def __init__(self, name, sex):
        self.name = name
        self.sex = sex

    def __str__(self):
        return 'name=%s, sex=%s'.format(self.name, self.sex)


if __name__ == '__main__':
    connection = sqlite3.connect('test.db')
    c = connection.cursor()

    dbUtil = DBUtil(c)
    for i in range(10):
        dbUtil.insert(Person('wanglilin{0}'.format(i), 1))
    connection.commit()

    dbUtil.query()

    dbUtil.update(Person('wanglilin4', 29))

    dbUtil.query()

    dbUtil.delete(Person('wanglilin0', 1))

    dbUtil.query()
    c.close()
    connection.close()
